package com.alipay.app.groupmeal.standard.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alipay.app.groupmeal.standard.R;
import com.alipay.app.groupmeal.standard.widget.business.BaseBScreenView;

/**
 * @ProjectName: 团餐
 * @ClassName: InputAmountView
 * @Description: java类作用描述
 * @Author: bd-001935
 * @CreateDate: 4/21/22 6:07 PM
 * @UpdateUser: bd-001935
 * @UpdateDate: 4/21/22 6:07 PM
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class InputAmountView extends BaseBScreenView {

    private TextView mTvSymbol;
    private InputAmountFitViewTextView mTvAmount;

    public InputAmountView(Context context) {
        this(context, null);
    }

    public InputAmountView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputAmountView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_input_amount_view;
    }

    @Override
    protected void initView(View view) {
        mTvSymbol = view.findViewById(R.id.tv_symbol);
        mTvAmount = view.findViewById(R.id.tv_custom_amount);
        setFocusable(false);
    }

    @Override
    protected void initData() {
        if (mTvAmount.getText().length() == 0) {
            mTvSymbol.setTextColor(getContext().getColor(R.color.txt_999999));
        } else
            mTvSymbol.setTextColor(getContext().getColor(R.color.txt_333333));

        mTvAmount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mWatcher != null)
                    mWatcher.onTextChanged(s, start, before, count);
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mTvAmount.getText().length() == 0) {
                    mTvSymbol.setTextColor(getContext().getColor(R.color.txt_999999));
                } else
                    mTvSymbol.setTextColor(getContext().getColor(R.color.txt_333333));
                if (mWatcher != null)
                    mWatcher.afterTextChanged(s);
            }
        });
    }

    public void setText(String s) {
        mTvAmount.setText(s);
//        if (TextUtils.isEmpty(s))
//            mTvAmount.invalidate();
    }

    public void clear() {
        mTvAmount.clear();
    }

    public String getText() {
        return mTvAmount.getText().toString();
//        return mTvAmount.getSumPrice();
    }
    public String getTextBySum() {
//        return mTvAmount.getText().toString();
        return mTvAmount.getSumPrice();
    }

    public void addTextChangedListener(CustomTextWatcher textWatcher) {
        mWatcher = textWatcher;
    }

    @Override
    public boolean onKeyEventNum(KeyEvent event) {
        return mTvAmount.dispatchKeyEvent(event);
    }

    private CustomTextWatcher mWatcher;

    public interface CustomTextWatcher {
        void onTextChanged(CharSequence s, int start, int before, int count);

        void afterTextChanged(Editable s);
    }
}
